﻿using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;

namespace Xioa_UI.Controls;

[TemplatePart(Name = SearchButton, Type = typeof(Button))]
[TemplatePart(Name = SearchText, Type = typeof(TextBox))]
public class XioaSearchBar : Button
{
    private const string SearchButton = "SearchButton";
    private const string SearchText = "SearchText";

    #region private filed

    private Button? _button;
    private TextBox? _textBox;

    #endregion

    public XioaSearchBar()
    {
    }

    public override void OnApplyTemplate()
    {
        if (_button is not null)
        {
            _button.Click -= OnButtonClick;
        }

        if (_textBox is not null)
        {
            _textBox.TextChanged -= _textBox_TextChanged;
            _textBox.TextChanged -= OnTextBoxOnTextChanged;
        }

        _button = GetTemplateChild(SearchButton) as Button;
        _textBox = GetTemplateChild(SearchText) as TextBox;


        _textBox.TextChanged += OnTextBoxOnTextChanged;
        _textBox.TextChanged += _textBox_TextChanged;
        _button.Click += OnButtonClick;
    }

    private void _textBox_TextChanged(object sender, TextChangedEventArgs e)
    {
        this.Text = _textBox.Text;
    }

    private void OnButtonClick(object sender, RoutedEventArgs e)
    {
        RoutedEventArgs newEventArgs = new RoutedEventArgs(XioaSearchBar.ClickEvent);
        RaiseEvent(newEventArgs);
    }

    private void OnTextBoxOnTextChanged(object sender, TextChangedEventArgs args)
    {
        RoutedEventArgs newEventArgs = new RoutedEventArgs(XioaSearchBar.TextChangedEvent);
        RaiseEvent(newEventArgs);
    }


    public static readonly DependencyProperty CornerRadiusProperty =
        ElementBase.Property<XioaSearchBar, CornerRadius>("CornerRadiusProperty");

    public CornerRadius CornerRadius
    {
        get => (CornerRadius)GetValue(CornerRadiusProperty);
        set => SetValue(CornerRadiusProperty, value);
    }


    public static readonly DependencyProperty IconSizeProperty =
        ElementBase.Property<XioaSearchBar, double>("IconSizeProperty");

    public double IconSize
    {
        get => (double)GetValue(IconSizeProperty);
        set => SetValue(IconSizeProperty, value);
    }

    public static readonly DependencyProperty TextSizeProperty =
        ElementBase.Property<XioaSearchBar, double>("TextSizeProperty");

    public double TextSize
    {
        get => (double)GetValue(TextSizeProperty);
        set => SetValue(TextSizeProperty, value);
    }

    public static readonly DependencyProperty HeightProperty =
        ElementBase.Property<XioaSearchBar, double>("HeightProperty");

    public double Height
    {
        get => (double)GetValue(HeightProperty);
        set { SetValue(HeightProperty, value); }
    }

    public static readonly DependencyProperty WidthProperty =
        ElementBase.Property<XioaSearchBar, double>("WidthProperty");

    public double Width
    {
        get => (double)GetValue(WidthProperty);
        set { SetValue(WidthProperty, value); }
    }

    public static readonly DependencyProperty TextProperty =
        DependencyProperty.Register("Text", typeof(string), typeof(XioaSearchBar),
            new PropertyMetadata(null, (s, e) => { ((XioaSearchBar)s).Text = (string)e.NewValue; }));

    public string Text
    {
        get => (string)GetValue(TextProperty);
        set { SetValue(TextProperty, value); }
    }

    public event RoutedEventHandler TextChanged
    {
        add { AddHandler(TextChangedEvent, value); }
        remove { RemoveHandler(TextChangedEvent, value); }
    }

    public static readonly RoutedEvent TextChangedEvent = EventManager.RegisterRoutedEvent("TextChanged",
        RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(XioaSearchBar));


    public event RoutedEventHandler Click
    {
        add { AddHandler(ClickEvent, value); }
        remove { RemoveHandler(ClickEvent, value); }
    }

    public static readonly RoutedEvent ClickEvent = EventManager.RegisterRoutedEvent("ClickEvent",
        RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(XioaSearchBar));
    
    
    public static readonly DependencyProperty MouseOverTrueBorderBrushProperty =
        ElementBase.Property<XioaSearchBar, Brush>("MouseOverTrueBorderBrushProperty");
    
    public Brush MouseOverTrueBorderBrush
    {
        get => (Brush)GetValue(MouseOverTrueBorderBrushProperty);
        set { SetValue(MouseOverTrueBorderBrushProperty, value); }
    }
    
    public static readonly DependencyProperty MouseOverFalseBorderBrushProperty =
        ElementBase.Property<XioaSearchBar, Brush>("MouseOverFalseBorderBrushProperty");
    
    public Brush MouseOverFalseBorderBrush
    {
        get => (Brush)GetValue(MouseOverFalseBorderBrushProperty);
        set { SetValue(MouseOverFalseBorderBrushProperty, value); }
    }
    
    
    public static readonly DependencyProperty IconFillBrushProperty =
        ElementBase.Property<XioaSearchBar, Brush>("IconFillBrushProperty");
    
    public Brush IconFillBrush
    {
        get => (Brush)GetValue(IconFillBrushProperty);
        set { SetValue(IconFillBrushProperty, value); }
    }
}